package model.game;

import java.util.ArrayList;
import java.util.List;

import model.area.Field;
import model.person.Person;

public class TeamManagement {
	private List<Team> teams = new ArrayList<Team>();
	private List<Match> matches = new ArrayList<Match>();

	public TeamManagement() {
	}

	public void addOwnTeam(String teamName) {
		teams.add(new OwnTeam(teamName));
	}

	public void addOtherTeam(String teamName) {
		teams.add(new OtherTeam(teamName));
	}

	public String getTeamData() {
		String data = "";
		for (Team t : teams) {
			TeamContext context = new TeamContext(t);
			data += context.getData();
		}
		return data;
	}

	public void addPersonToOwnTeam(Person person, String teamName) {
		for (Team t : teams) {
			if (t.getName().equals(teamName)) {
				((OwnTeam) t).addPerson(person);
			}
		}
	}

	public Team getTeam(String teamName) {
		for (Team t : teams) {
			if (t.getName().equals(teamName)) {
				return t;
			}
		}
		return null;
	}

	public void addMatch(Team tA, Team tB, Field f, String startTime) {
		matches.add(new Match(tA, tB, f, startTime));
	}

	public void addGoal(String teamName, String startTime) {
		System.out.println(startTime.toString());
		Match m = getMatchByNameAndStartTime(teamName, startTime);
		if (m.getStartTime().equals(startTime)) {
			if (m.getTeamA().equals(teamName)) {
				m.addGoalTeamA();
			} else {
				m.addGoalsTeamB();
			}
		}
	}

	public Match getMatchByNameAndStartTime(String teamName, String startTime) {
		for (Match m : matches) {
			if (m.getStartTime().equals(startTime)) {
				if (m.getTeamA().getName().equals(teamName)
						|| m.getTeamB().getName().equals(teamName)) {
					return m;
				}
			}
		}
		return null;
	}

	public void addRefereeToMatch(Match m, Person p) {
		m.addReferee(p);
	}
}
